<?php
	!defined('DYHB_PATH')&&exit;
	class UserController extends InitController{
		public function register(){
			if($GLOBALS['___login___']!=false){
				$this->assign('__JumpUrl__',Dyhb::U('index/index'));
				$this->E('你已经是注册会员');
			}
			$this->assign('title','博客用户注册');
			$this->display();
		}
		/*用户注册提交*/
		public function submit(){
			$city=G::getGpc('province','p');
			$city.=' '.G::getGpc('city','p');
			if($GLOBALS['___login___']!=false){
				$this->E('你已经是注册会员');
			}
			$oModel=new UserModel();
			$oModel->regname=G::getGpc('user_name','p');
			$oModel->regrealname=G::getGpc('user_realname','p');
			$oModel->regpassword=md5(G::getGpc('user_password1','p').ENCODE);
			$oModel->regemail=G::getGpc('user_email','p');
			$oModel->regcity=$city;
			$oModel->regphoto=str_replace('\\','/',BLOG_PATH.'/data/upload/blogger.gif');
			$oModel->save();
			if(!$oModel->isError()){
					$this->assign('__JumpUrl__',Dyhb::U('index/index'));
					$this->S('注册成功');
			}else{
					$this->E($oModel->getErrorMessage());
			}
		}
		/*用户登录*/
		public function login(){
			if($GLOBALS['___login___']!=false){
				$this->E('你已经是注册会员');
			}
			$userName=G::getGpc('user_name','p');
			$userPassword=G::getGpc('user_password','p');
			$string=G::getGpc('string','p');
			if(empty($userName)){
				$this->E("用户名不能为空");
			}
			if(empty($userPassword)){
				$this->E("密码不能为空");
			}
			if(empty($string)){
				$this->E("请输入验证码");
			}elseif($_SESSION['c_string']!=strtolower($string)){
				$this->E("验证码错误");
			}
			$userPassword=md5($userPassword.ENCODE);
			$arrDate=array('regname'=>$userName,'regpassword'=>$userPassword);
			$oUser=UserModel::F($arrDate)->getOne();
			if(empty($oUser->user_id)){
				$this->E("用户名或密码错误");
			}else{

				 Dyhb::cookie('user_login',$oUser->toArray());
				 $this->assign("seconds",2);
				$this->assign('__JumpUrl__',Dyhb::U('index/index'));
				$this->S(sprintf("hello %s,登陆成功",$userName));
			}
		}
		/*注销用户
		*/
		public function logout(){
			if($GLOBALS['___login___']){
				Dyhb::cookie('user_login',array());
				$this->assign("seconds",1);
				$this->assign("__JumpUrl__",Dyhb::U('index/index'));
				$this->S('登出成功');
			}else{
				$this->assign("seconds",1);
				$this->E('你已经成功登出');
			}
		}
		/*显示用户最新的文章*/
		public function showarticle(){
			$arr=array('article_author'=>G::getGpc('gid','g'));
			$currentPage=G::getGpc('page','g');
			if(empty($currentPage)){
			 $currentPage=1;
			}
			$Nums=ArticleModel::F($arr)->all()->getCounts();
			$pageSize=5;
			$subNums=4;
			$subLink=Dyhb::U('user/showarticle?page=');
			$subType=2;
			$oPage=new myPage($Nums,$pageSize,$currentPage,$subNums,$subLink,$subType);

			$start=($currentPage-1)*$pageSize;
			if($pageSize>$Nums){
				$pageSize=$Nums;
			}
			$arr=ArticleModel::F($arr)->order('article_id DESC')->limit($start,$pageSize)->getAll();
			$this->assign('oPage',$oPage);
			$this->assign('article_list',$arr);
			$this->assign('title',G::getGpc('gid','g').'博客文章');
			$this->display();
		}
		public function checkuser(){
			if($GLOBALS['___login___']!=false){
				$this->E('你已经是注册会员');
			}
			$name=G::getGpc('value','g');
			$arr=array('regname'=>$name);
			$oUser=UserModel::F($arr)->query();
			if(!empty($oUser->user_id)){
				echo 1;
			}else{
				echo 0;
			}
		}
		public function checkemail(){
			if($GLOBALS['___login___']!=false){
				$this->E('你已经是注册会员');
			}
			$email=G::getGpc('value','g');
			$arr=array('regemail'=>$email);
			$oUser=UserModel::F($arr)->query();
			if(!empty($oUser->user_id)){
				echo 1;
			}else{
				echo 0;
			}
		}
		public function checkstring(){
			if($GLOBALS['___login___']!=false){
				$this->E('你已经是注册会员');
			}
			$string=G::getGpc('value','g');
			if($string != $_SESSION['c_string']){
				echo 1;
			}else{
				echo 0;
			}
		}
	}
?>